package com.LachesismStorm.lachesism.member.controller;

import com.LachesismStorm.lachesism.core.common.response.ResponsePage;
import com.LachesismStorm.lachesism.core.common.response.ResponseResult;
import com.LachesismStorm.lachesism.core.email.EmailUtil;
import com.LachesismStorm.lachesism.core.util.LachesismTools;
import com.LachesismStorm.lachesism.member.dto.ConstellationDTO;
import com.LachesismStorm.lachesism.member.service.YunOService;
import com.LachesismStorm.lachesism.member.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import static com.LachesismStorm.lachesism.core.common.enums.ResponseCodeEnum.FAILED;

/**
 * @Author lachesism
 * @Date 2024-04-10 16:05
 **/
@RestController
@Api(tags = "通用")
@RequestMapping("/lachesism")
public class YunOController {
    @Resource
    private YunOService yunoService;

    @Resource
    private EmailUtil emailUtil;

    @ApiOperation(value = "今天吃什么")
    @PostMapping("/getEat")
    public ResponseResult getEat() {
        List<String> foodList = yunoService.getEat();
        return ResponseResult.success(foodList);
    }

    @ApiOperation(value = "当前金价")
    @PostMapping("/getGoldPrice")
    public ResponseResult getGoldPrice() throws IOException {
        String price = yunoService.getGoldPrice();
        return ResponseResult.success(price);
    }

    @ApiOperation(value = "历史金价")
    @PostMapping("/getGoldPriceHistory")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "GoldPrice", value = "时间范围类型")
    })
    public ResponseResult getGoldPriceHistory(@RequestBody GoldPrice time) {
        List<GoldPrice> priceList = yunoService.getGoldPriceHistory(time);
        return ResponseResult.success(priceList, "查询成功");
    }

    @ApiOperation(value = "当天金价")
    @PostMapping("/getGoldPriceToday")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "GoldPrice", value = "时间范围类型")
    })
    public ResponseResult getGoldPriceToday(@RequestBody GoldPrice time) {
        List<GoldPrice> priceList = yunoService.getGoldPriceToday(time);
        return ResponseResult.success(priceList, "查询成功");
    }

    @ApiOperation(value = "指定时间最后价格")
    @PostMapping("/getLastPrice")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "GoldPrice", value = "时间")
    })
    public ResponseResult getLastPrice(@RequestBody GoldPrice time) {
        BigDecimal price = yunoService.getLastPrice(time);
        return ResponseResult.success(price, "查询成功");
    }

    @ApiOperation(value = "查找地区code")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "city", value = "城市"),
            @ApiImplicitParam(paramType = "query", name = "area", value = "地区")
    })
    @PostMapping("/getAreaCode")
    public ResponseResult getAreaCode(@RequestParam String city, @RequestParam String area) {
        String code = yunoService.getAreaCode(city, area);
        if (code == null) {
            return ResponseResult.failed(FAILED, null);
        }
        return ResponseResult.success(code);
    }

    @ApiOperation(value = "添加地区code")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "city", value = "城市"),
            @ApiImplicitParam(paramType = "query", name = "area", value = "地区"),
            @ApiImplicitParam(paramType = "query", name = "code", value = "编码"),
    })
    @PostMapping("/addAreaCode")
    public ResponseResult addAreaCode(@RequestParam String city, @RequestParam String area, @RequestParam String code) {
        yunoService.addAreaCode(city, area, code);
        return ResponseResult.success(null, "添加成功");
    }


    @ApiOperation(value = "查询天气")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "code", value = "编码"),
    })
    @PostMapping("/getWeather")
    public ResponseResult getWeather(@RequestParam String code) {
        List<WeatherVO> weather = yunoService.getWeather(code);
        if (weather == null) {
            return ResponseResult.failed("操作失败");
        }
        return ResponseResult.success(weather, "查找成功");
    }

    @ApiOperation(value = "添加天气")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "CityWeatherVO", value = "城市天气信息"),
    })
    @PostMapping("/addWeather")
    public ResponseResult addWeather(@RequestBody CityWeatherVO cityWeatherInfo) {
        yunoService.addWeather(cityWeatherInfo);
        return ResponseResult.success(null, "添加成功");
    }


    @ApiOperation(value = "查询家训")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "大小"),
    })
    @PostMapping("/getFamilyMotto")
    public ResponseResult getFamilyMotto(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        ResponsePage<StringInfoVO> mottoList = yunoService.getFamilyMotto(pageNum, pageSize);
        return ResponseResult.success(mottoList, "查询成功");
    }

    @PostMapping("/getCatalogInfo")
    public ResponseResult getCatalogInfo() {
        List<String> infoList = yunoService.getCatalogInfo();
        return ResponseResult.success(infoList, "查询成功");
    }

    @ApiOperation(value = "获取消息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "大小"),
    })
    @PostMapping("/getMessageList")
    public ResponseResult getMessageList(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        ResponsePage<MessageVO> messageList = yunoService.getMessageList(pageNum, pageSize);
        return ResponseResult.success(messageList, "查询成功");
    }

    @ApiOperation(value = "获取答案")
    @PostMapping("/getAnswer")
    public ResponseResult getAnswer() {
        Answer answer = yunoService.getAnswer();
        return ResponseResult.success(answer, "查询成功");
    }

    @ApiOperation(value = "获取奥特曼列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "大小"),
    })
    @PostMapping("/getAnimatedCharacterList")
    public ResponseResult getUltramanList(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        ResponsePage<AnimatedCharacters> characterList = yunoService.getAnimatedCharacterList(pageNum, pageSize);
        return ResponseResult.success(characterList, "查询成功");
    }

    @ApiOperation(value = "获取奥特曼列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "ConstellationParam", value = "出生时间")
    })
    @PostMapping("/getConstellation")
    public ResponseResult getConstellation(@RequestBody ConstellationParam constellationParam) {
        List<String> constellationInfo = yunoService.getConstellation(constellationParam);
        return ResponseResult.success(constellationInfo, "查询成功");
    }

    @ApiOperation(value = "检查是否重复code")
    @PostMapping("/checkRepeatCode")
    public ResponseResult checkRepeatCode(@RequestBody NoteVO note) {
        Boolean result = yunoService.checkRepeatCode(note);
        return ResponseResult.success(result, "保存成功");
    }

    @ApiOperation(value = "保存纸条")
    @PostMapping("/addNote")
    public ResponseResult sendAMessage(@RequestBody NoteVO note) {
        yunoService.addNote(note);
        return ResponseResult.success(null, "保存成功");
    }

    @ApiOperation(value = "取纸条")
    @PostMapping("/getNote")
    public ResponseResult getNote(@RequestParam String code) {
        NoteVO note = yunoService.getNote(code);
        return ResponseResult.success(note, "查询成功");
    }

    @ApiOperation(value = "口令上传文件")
    @PostMapping("/uploadFile")
    public ResponseResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam String code) {
        yunoService.uploadFile(file, code);
        return ResponseResult.success(null, "上传成功");
    }

    @ApiOperation(value = "口令查询文件")
    @PostMapping("/getFilePath")
    public ResponseResult getFilePath(@RequestParam String code) {
        String path = yunoService.getFilePath(code);
        return ResponseResult.success(path, "查询成功");
    }

    @ApiOperation(value = "获取数据库文件信息")
    @PostMapping("/getDBFileInfo")
    public ResponseResult getDBFileInfo(@RequestParam Integer type) {
        List<NoteVO> noteList = yunoService.getDBFileInfo(type);
        return ResponseResult.success(noteList, "查询成功");
    }

    @ApiOperation(value = "删除数据库文件")
    @PostMapping("/deleteDBFile")
    public ResponseResult deleteDBFile(@RequestParam Integer type, @RequestParam Integer id) {
        yunoService.deleteDBFile(type, id);
        return ResponseResult.success(null, "删除成功");
    }

    @ApiOperation(value = "查询待办事项列表")
    @PostMapping("/getToDoList")
    public ResponseResult getToDoList(@RequestBody ToDoS todos) {
        ResponsePage<ToDoS> todoList = yunoService.getToDoList(todos);
        return ResponseResult.success(todoList, "查询成功");
    }

    @ApiOperation(value = "更新待办事项")
    @PostMapping("/updateToDos")
    public ResponseResult updateToDos(@RequestBody ToDoS todos) {
        yunoService.updateToDos(todos);
        return ResponseResult.success(null, "操作成功");
    }

    @ApiOperation(value = "创建待办事项")
    @PostMapping("/createToDoThing")
    public ResponseResult createToDoThing(@RequestBody ToDoS todos) {
        yunoService.createToDoThing(todos);
        return ResponseResult.success(null, "创建成功");
    }

    @ApiOperation(value = "查询账单")
    @PostMapping("/getBillList")
    public ResponseResult getBillList(@RequestBody Bill bill) {
        List<Bill> billList = yunoService.getBillList(bill);
        return ResponseResult.success(billList, "查询成功");
    }

    @ApiOperation(value = "修改账单")
    @PostMapping("/updateBill")
    public ResponseResult updateBill(@RequestBody Bill bill) {
        yunoService.updateBill(bill);
        return ResponseResult.success(null, "操作成功");
    }

    @ApiOperation(value = "添加账单")
    @PostMapping("/addBill")
    public ResponseResult addBill(@RequestBody Bill bill) {
        Integer id = yunoService.addBill(bill);
        return ResponseResult.success(id, "添加成功");
    }

    @ApiOperation(value = "查询粘贴板列表")
    @PostMapping("/getPasteboardList")
    public ResponseResult getPasteboardList(@RequestBody Pasteboard pasteboard) {
        List<Pasteboard> pasteboardList = yunoService.getPasteboardList(pasteboard);
        return ResponseResult.success(pasteboardList, "查询成功");
    }

    @ApiOperation(value = "删除粘贴板")
    @PostMapping("/deletePasteboard")
    public ResponseResult deletePasteboard(@RequestBody Pasteboard pasteboard) {
        yunoService.deletePasteboard(pasteboard.getId());
        return ResponseResult.success(null, "操作成功");
    }

    @ApiOperation(value = "新增粘贴板")
    @PostMapping("/addPasteboard")
    public ResponseResult addPasteboard(@RequestBody Pasteboard pasteboard) {
        Integer id = yunoService.addPasteboard(pasteboard);
        return ResponseResult.success(id, "添加成功");
    }

    @ApiOperation(value = "获取随机刑法")
    @PostMapping("/getRandomCriminalLaws")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "type", value = "类型")
    })
    public ResponseResult getRandomCriminalLaws(@RequestParam Integer type) {
        List<StringInfoVO> laws = yunoService.getRandomCriminalLaws(type);
        return ResponseResult.success(laws, "添加成功");
    }

    @ApiOperation(value = "模糊查询刑法")
    @PostMapping("/fuzzyGetCriminalLaw")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "StringInfoVO", value = "关键词")
    })
    public ResponseResult fuzzyGetCriminalLaw(@RequestBody StringInfoVO keyWord) {
        List<StringInfoVO> laws = yunoService.fuzzyGetCriminalLaw(keyWord);
        return ResponseResult.success(laws, "添加成功");
    }

    @ApiOperation(value = "获取随机单词")
    @PostMapping("/getWords")
    public ResponseResult getWords() {
        List<Word> words = yunoService.getWords();
        return ResponseResult.success(words, "查询成功");
    }

    @ApiOperation(value = "查询单词")
    @PostMapping("/getWordByKeyword")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "Word", value = "单词")
    })
    public ResponseResult getWordByKeyword(@RequestBody Word word) {
        List<Word> words = yunoService.getWordByKeyword(word);
        return ResponseResult.success(words, "查询成功");
    }

    @ApiOperation(value = "扫码上传文件")
    @PostMapping("/scanUpload")
    public ResponseResult scanUpload(@RequestParam("file") MultipartFile file, @RequestParam String sessionId) {
        yunoService.scanUpload(file, sessionId);
        return ResponseResult.success(null, "上传成功");
    }

    @ApiOperation(value = "上传临时文件")
    @PostMapping("/uploadTempFile")
    public ResponseResult uploadTempFile(@RequestParam("file") MultipartFile file) {
        String name = yunoService.uploadTempFile(file);
        return ResponseResult.success(name, "上传成功");
    }

    @ApiOperation(value = "查询家政文档")
    @PostMapping("/getHousekeepingWiki")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "StringInfoVO", value = "关键词")
    })
    public ResponseResult getHousekeepingWiki(@RequestBody StringInfoVO keyWord) {
        List<Word> wiki = yunoService.getHousekeepingWiki(keyWord);
        return ResponseResult.success(wiki, "查询成功");
    }

    @ApiOperation(value = "添加家政文档")
    @PostMapping("/addHousekeepingWiki")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "Word", value = "内容")
    })
    public ResponseResult addHousekeepingWiki(@RequestBody Word word) {
        yunoService.addHousekeepingWiki(word);
        return ResponseResult.success(null, "添加成功");
    }

    @ApiOperation(value = "添加备忘录")
    @PostMapping("/addMemorandum")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "MemorandumVO", value = "内容")
    })
    public ResponseResult addMemorandum(@RequestBody MemorandumVO memorandum) {
        yunoService.addMemorandum(memorandum);
        return ResponseResult.success(null, "添加成功");
    }

    @ApiOperation(value = "查询备忘录")
    @PostMapping("/getMemorandum")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "MemorandumVO", value = "内容")
    })
    public ResponseResult getMemorandum(@RequestBody MemorandumVO memorandum) {
        List<MemorandumVO> memorandumList = yunoService.getMemorandum(memorandum);
        return ResponseResult.success(memorandumList, "查询成功");
    }

    @ApiOperation(value = "修改备忘录")
    @PostMapping("/updateMemorandum")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "MemorandumVO", value = "内容")
    })
    public ResponseResult updateMemorandum(@RequestBody MemorandumVO memorandum) {
        yunoService.updateMemorandum(memorandum);
        return ResponseResult.success(null, "修改成功");
    }

    //    @GetMapping("/getCrazy")
//    public ResponseResult getCrazy() {
//        System.out.println("开始获取疯狂星期四");
//        String path = "/project/media/file/crazyThursday.txt";
//        try (BufferedWriter writer = new BufferedWriter(new FileWriter(path, true))) {
//            String s = LachesismTools.sendGet("https://api.shadiao.pro/kfc", null, null); // 移除了错误的字符
//            JSONObject jsonObject = new JSONObject(s);
//            String text = jsonObject.getJSONObject("data").getString("text");
//            writer.write(text);
//            writer.newLine(); // 添加换行
//            System.out.println(text);
//        } catch (Exception e) {
//            System.out.println("获取数据或写入文件时出现问题：" + e.getMessage());
//            e.printStackTrace();
//        }
//        return ResponseResult.success(null, "成功");
//    }
    @GetMapping("/getIp")
    public ResponseResult test() throws Exception {
        String ip = LachesismTools.getRealRequestIp();
        return ResponseResult.success(ip, "成功");
    }

    @ApiOperation(value = "获取每日视频")
    @PostMapping("/getDailyVideo")
    public ResponseResult getDailyVideoList() {
        List<String> videoList = yunoService.getDailyVideoList();
        return ResponseResult.success(videoList, "查询成功");
    }


    @ApiOperation(value = "获取星座运势")
    @PostMapping("/getConstellationFortune")
    public ResponseResult getConstellationFortune() {
        List<ConstellationDTO> constellationList = yunoService.getConstellationFortune();
        return ResponseResult.success(constellationList, "查询成功");
    }


    @ApiOperation(value = "获取星座运势")
    @PostMapping("/getAreaList")
    public ResponseResult getAreaList() {
        List<SysArea> areList = yunoService.getAreaList();
        return ResponseResult.success(areList, "查询成功");
    }

    @ApiOperation(value = "获取今天的老黄历信息")
    @PostMapping("/getLunarInfo")
    public ResponseResult getLunarInfo() {
        ExtLunar lunarInfo = yunoService.getLunarInfo();
        return ResponseResult.success(lunarInfo, "查询成功");
    }

    @ApiOperation(value = "获取建议列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "第几页"),
    })
    @PostMapping("/getSuggestionList")
    public ResponseResult getSuggestionList(@RequestParam Integer pageNum) {
        ResponsePage<SysSuggestions> suggestions = yunoService.getSuggestionList(pageNum);
        return ResponseResult.success(suggestions, "查询成功");
    }

    @ApiOperation(value = "添加建议")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "SysSuggestions", value = "内容"),
    })
    @PostMapping("/addSuggestion")
    public ResponseResult getSuggestionList(@RequestBody SysSuggestions suggestion) {
        yunoService.addSuggestion(suggestion);
        return ResponseResult.success(null, "添加成功");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "food", value = "食物内容"),
    })
    @PostMapping("/addEat")
    public ResponseResult addEat(@RequestParam String food) {
        yunoService.addEat(food);
        return ResponseResult.success(null, "添加成功");
    }

    
}
